package com.gg.hj.service.impl;

import com.gg.common.mapper.SysMenusMapper;
import com.gg.common.pojo.SysMenus;
import com.gg.common.view.ParentMenuView;
import com.gg.hj.service.SysMenusService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysMenusServiceImpl implements SysMenusService {
    @Resource
    private SysMenusMapper menusMapper;
    @Override
    // TODO 查询登入人具有的角色 角色具有的菜单
    public List<ParentMenuView> menusService(int userId) throws Exception {
        return this.menusMapper.menus(userId);
    }

    @Override
    //TODO 查询登入人具有的角色 角色具有的菜单
    public  Map<Integer,Map<String,List<SysMenus>>> menuMap(int userId) throws Exception {

        Map<Integer,Map<String,List<SysMenus>>> Map = new LinkedHashMap<Integer,Map<String,List<SysMenus>>>();
        List<SysMenus> sysMenus = this.menusMapper.searchQueryMenuAll(userId);
        for(int i=0; i<sysMenus.size(); i++){
            SysMenus p1 = sysMenus.get(i);
            if(p1.getParentId()==0){
                Map<String,List<SysMenus>> menuMap = new LinkedHashMap<String,List<SysMenus>>();
                for(int j=0;j<sysMenus.size();j++){
                    SysMenus p2 = sysMenus.get(j);
                    List<SysMenus> menus = new ArrayList<SysMenus>();
                    if(p1.getId()==p2.getParentId()){
                        for(int z=0;z<sysMenus.size();z++){
                            SysMenus p3 = sysMenus.get(z);
                            if(p2.getId()==p3.getParentId()){
                                menus.add(p3);
                            }
                        }
                        menuMap.put(p2.getName(),menus);
                    }
                    Map.put(p1.getId(),menuMap);
                }
            }
        }
        return Map;
    }
}
